package com.zxl.always.on.display.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.base.BaseAppCompatActivity;
import com.zxl.always.on.display.constant.ICustomAction;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.track.MobclickAgentHelper;
import com.zxl.always.on.display.track.StatisticsConstant;
import com.zxl.always.on.display.track.StatisticsType;
import com.zxl.always.on.display.ui.helper.TabViewHelper;
import com.zxl.always.on.display.ui.widget.SettingPreviewItemView;
import com.zxl.always.on.display.ui.widget.clock.BaseClockView;

/**
 * Created by Administrator on 2018/3/2.
 */

public class ClockSettingsActivity extends BaseAppCompatActivity implements View.OnClickListener {

    private ClockStyle clockStyle;

    private TabViewHelper mTabViewHelper;

    private BaseClockView mBaseClockView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock_settings);
        clockStyle = (ClockStyle) getIntent().getSerializableExtra("data");

        findViewById(R.id.settings_custom_action_bar_cancel_button).setOnClickListener(this);
        findViewById(R.id.settings_custom_action_bar_apply_button).setOnClickListener(this);
        findViewById(R.id.settings_custom_preview_container).setOnClickListener(this);

        LinearLayout previewLayout = (LinearLayout) findViewById(R.id.settings_custom_preview_container);
        mBaseClockView = (BaseClockView) getLayoutInflater().inflate(clockStyle.getLayout(), previewLayout, false);
        previewLayout.addView(mBaseClockView);
        mTabViewHelper = new TabViewHelper(this);
        mTabViewHelper.initView(mBaseClockView, clockStyle);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.settings_custom_preview_container:
                mTabViewHelper.hideTabContent();
                break;
            case R.id.settings_custom_action_bar_apply_button:
                mTabViewHelper.saveData();
                sendBroadcast(new Intent(ICustomAction.ACTION_CLOCK_CHANGE).putExtra("data", clockStyle));
                finish();
                MobclickAgentHelper.onEvent(this, StatisticsType.SETTINGS, StatisticsConstant.Event.APPLY, clockStyle.name());
                break;
            case R.id.settings_custom_action_bar_cancel_button:
                finish();
                MobclickAgentHelper.onEvent(this, StatisticsType.SETTINGS, StatisticsConstant.Event.CANCEL, clockStyle.name());
                break;
        }
    }

    public static void startPage(Activity activity, SettingPreviewItemView itemView) {
        activity.startActivity(new Intent(activity, ClockSettingsActivity.class).putExtra("data", itemView.getClockStyle()));
    }
}
